package com.yyq.adms.util;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.lang.ObjectUtils;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import com.yyq.adms.sysmanage.pojo.DataDictionary;
import com.yyq.adms.sysmanage.pojo.MethodBean;
import com.yyq.sos.util.ObjectUtil;

public class ValidateUtil {

	private static ValidateUtil vu = new ValidateUtil();
	private static final String filePath = "com/yyq/adms/validateConf";
	
	private ValidateUtil(){
		
	}
	
	public static ValidateUtil getInstance(){
		return vu;
	}
	
	public synchronized  Map<String,ValidateVo> getValidatesByClassName(String className){
		Map<String,ValidateVo> mvv = new LinkedHashMap<String,ValidateVo>();
		InputStream is = null;
		try {
			SAXReader saxReader = new SAXReader();
			is = ValidateUtil.class.getClassLoader().getResourceAsStream(filePath+File.separator+className+".xml");
		    Document document = saxReader.read(is);
		    Element root = document.getRootElement();
		    Iterator<Element> items = root.elementIterator("items");
		    System.out.println(root.getName());
		    System.out.println(is==null);
		    if(items.hasNext()){
		    	Element item = items.next();
		    	Iterator<Element>  children= item.elementIterator();
		    	while(children.hasNext()){
		    		ValidateVo vvo = new ValidateVo();
		    		Element child = children.next();
		    		vvo.setCname(ObjectUtils.toString(child.element("cname").getText()));
		    		vvo.setName(ObjectUtils.toString(child.element("name").getText()));
		    		vvo.setMaxlen(Integer.parseInt(ObjectUtils.toString(child.element("maxlen").getText())
		    				.equals("")?"0":ObjectUtils.toString(child.element("maxlen").getText())));
		    		vvo.setDateFormat(ObjectUtils.toString(child.element("dateFormat").getText()));
		    		vvo.setMerge(Boolean.parseBoolean(ObjectUtils.toString(child.element("isMerge").getText())));
		    		vvo.setType(ObjectUtils.toString(child.element("type").getText()));
		    		vvo.setSentDep(Boolean.parseBoolean(ObjectUtils.toString(child.element("isSentDep").getText())));
		    		vvo.setUniquene(Boolean.parseBoolean(ObjectUtils.toString(child.element("isUniquene").getText())));
		    		if(child.element("isCombineUniquene") != null){
		    			vvo.setCombineUniquene(Boolean.parseBoolean(ObjectUtils.toString(child.element("isCombineUniquene").getText())));
		    		}
		    		vvo.setNull(Boolean.parseBoolean(ObjectUtils.toString(child.element("isNull").getText())));
		    		if(child.element("select")!=null){
		    			List<DataDictionary> dd = new ArrayList<DataDictionary>();
		    			Iterator<Element>  options = child.element("select").elementIterator();
		    			while(options.hasNext()){
		    				DataDictionary d = new DataDictionary();
		    				Element option = options.next();
		    				d.setText(option.getText());
		    				d.setValue(option.attributeValue("value"));
		    				dd.add(d);
		    			}
		    			vvo.setDd(dd);
		    		}
		    		mvv.put(vvo.getCname(), vvo);
		    	}
		    	
		    }
		   
		}catch(Exception e){
			e.printStackTrace();
		} finally {
			try {
				if(is != null){
					is.close();
					is = null;
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return mvv;
	}
	
	public static Map<String,MethodBean> getMethodMap(Map<String,ValidateVo> vvm,Class cls){
		Map<String,MethodBean> mbm = new HashMap<String,MethodBean>();
		Set<String> kset = vvm.keySet();
		for (String key : kset) {
			ValidateVo vo = vvm.get(key);
			String name = vo.getName().substring(0,1).toUpperCase()+vo.getName().substring(1);
			MethodBean mb = new MethodBean();
			if(vo.isMerge()){
				Method gm = ObjectUtil.getMethod(cls, "get"+name, null);
				mb.setGMethod(gm);
			}
			Method sm = ObjectUtil.getMethod(cls, "set"+name, new Class[]{getClassByType(vo.getType())});
			mb.setSMethod(sm);
			mb.setType(vo.getType());
			mb.setDateFormat(vo.getDateFormat());
			mb.setName(vo.getName());
			mbm.put(vo.getName(), mb);
		}
		
		//Method gExcelIndexMsg = ObjectUtil.getMethod(cls, "getExcelIndexMsg", null);
		Method sExcelIndexMsg = ObjectUtil.getMethod(cls, "setExcelIndexMsg", new Class[]{String.class});
		MethodBean mb = new MethodBean();
		mb.setSMethod(sExcelIndexMsg);
		mbm.put("excelIndexMsg", mb);
		
		return mbm;
	}
	
	public static Class getClassByType(String name){
		if("INT".equals(name)){
			return Integer.class;
		}else if("LONG".equals(name)){
			return Long.class;
		}else if("DATE".equals(name)){
			return Date.class;
		}else if("STR".equals(name)){
			return String.class;
		}else if("FLT".equals(name)){
			return Float.class;
		}else{
			return null;
		}
	}
	
	public static void main(String[] args) throws Exception {
		/*String filePath = "com/yyq/adms/validateConf/com.yyq.adms.gf.pojo.Ownlaw.xml";
		InputStream is = ValidateUtil.class.getClassLoader().getResourceAsStream(filePath);
		File file = new File(filePath);
		byte[] b = new byte[1024];
		int len = is.read(b);
		System.out.println(len);
		System.out.println(file.exists());*/
		Map map = ValidateUtil.getInstance().getValidatesByClassName("com.yyq.adms.gf.pojo.Ownlaw");
		System.out.println(map.size());
		
		
	}
}